package com.zhangchi.zoombar;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.HorizontalScrollView;


public class RotateScrollScale extends HorizontalScrollView implements Rotatable{
    private int mOrientation;
    protected View mChild;

    private boolean mEnableAnimation = true;
    private int mTargetDegree = 0;
    private int mStartDegree = 0;
    private int mCurrentDegree = 0;
    private long mAnimationStartTime = 0;
    private boolean mClockwise = false;
    private long mAnimationEndTime = 0;
    private static final int ANIMATION_SPEED = 270;
    private int mScreenHintWidth;


    public RotateScrollScale(Context context){
        super(context);
    }

    public RotateScrollScale(Context context, AttributeSet attrs){
        super(context, attrs);
    }

    public RotateScrollScale(Context context, AttributeSet attrs, int defStyleAttr){
        super(context, attrs, defStyleAttr);
    }

    public RotateScrollScale(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes){
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setScreenHintWidth(int screenHintWidth){
        mScreenHintWidth = screenHintWidth;
    }

    @Override
    public void setOrientation(int degree,boolean animation){
        mEnableAnimation = animation;

        degree = degree >= 0 ? degree % 360 : degree % 360 + 360;
        if (degree == mTargetDegree) {
            return;
        }

        mTargetDegree = degree;
        if (mEnableAnimation) {
            mStartDegree = mCurrentDegree;
            mAnimationStartTime = AnimationUtils.currentAnimationTimeMillis();

            int diff = mTargetDegree - mCurrentDegree;
            diff = diff >= 0 ? diff : 360 + diff; // make it in range [0, 359]


            diff = diff > 180 ? diff - 360 : diff;

            mClockwise = diff >= 0;
            mAnimationEndTime = mAnimationStartTime + Math.abs(diff) * 1000 / ANIMATION_SPEED;
        } else {
            mCurrentDegree = mTargetDegree;
        }

        invalidate();
    }

    protected void onDraw(Canvas canvas) {
        if (mCurrentDegree != mTargetDegree) {
            long time = AnimationUtils.currentAnimationTimeMillis();
            if (time < mAnimationEndTime) {
                int deltaTime = (int) (time - mAnimationStartTime);
                int degree = mStartDegree + ANIMATION_SPEED * (mClockwise ? deltaTime : -deltaTime)
                        / 1000;
                degree = degree >= 0 ? degree % 360 : degree % 360 + 360;
                mCurrentDegree = degree;
                invalidate();
            } else {
                mCurrentDegree = mTargetDegree;
            }
        }
        int left = getPaddingLeft();
        int top = getPaddingTop();
        int right = getPaddingRight();
        int bottom = getPaddingBottom();
        int width = getWidth() - left - right;
        int height = getHeight() - top - bottom;
        int saveCount = canvas.getSaveCount();
        canvas.rotate(-mCurrentDegree,780,getHeight()/2);
        super.onDraw(canvas);
        canvas.restoreToCount(saveCount);
    }

    //+ ExtB ASHA-10496,nimeng.wt,modify,2021-03-25,scaleview display error
    @Override
    public void fling(int velocity) {
        super.fling(velocity / 1000);
    }
    //- ExtB ASHA-10496,nimeng.wt,modify,2021-03-25,scaleview display error
}
